<!--生成凭证-->
<template>
    <a-modal v-model="showAddTable" :width="540"
             title="生成凭证"
             :maskClosable="false"
             :destroyOnClose="true" @cancel="cancelApply">
        <a-form :form="form" @keydown.native.enter.prevent="saveApply" class="modelSearch">
            <a-form-item v-bind="formStyle" label="凭证名称" required>
                <a-input ref="refocus"
                        placeholder="请输入凭证名称"
                        v-decorator="[
                        'CommonName',
                        {
                            rules: [
                                { required: true, message: '请输入凭证名称' },
                            ],
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }
                    ]"
                />
            </a-form-item>
            <a-form-item v-bind="formStyle" label="所属组织" required>
                <a-input
                        placeholder="请输入所属组织"
                        v-decorator="[
                        'OrganizationName',
                        {
                            rules: [
                                { required: true, message: '请输入所属组织' },
                            ],
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }
                    ]"
                />
            </a-form-item>
            <a-form-item label="国家" v-bind="formStyle" required>
                <a-select v-decorator="['Country',{ initialValue: 'CN'}, { rules: [{ required: true, message: '国家' }] }]">
                    <a-select-option value="CN" key="CN">CN</a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item label="省区" v-bind="formStyle" required>
                <a-select :getPopupContainer="trigger => trigger.parentNode"
                          placeholder="请选择省区"  @change="checkSqFun"
                          v-decorator="['StateOrProvince', { rules: [{ required: true, message: '请选择省区' }] }]">
                    <a-select-option v-for="item in pcaCodeList" :value="item.code" :key="item.code">{{
                        item.name }}
                    </a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item label="市县" v-bind="formStyle" required>
                <a-select :getPopupContainer="trigger => trigger.parentNode"
                          placeholder="请选择省区"
                          v-decorator="['LocalityName', { rules: [{ required: true, message: '请选择市县' }] }]">
                    <a-select-option v-for="item in pcaCodeChildrenList" :value="item.code" :key="item.code">{{
                        item.name }}
                    </a-select-option>
                </a-select>
            </a-form-item>
            <a-form-item v-bind="formStyle" label="机构" required>
                <a-input
                        placeholder="请输入机构"
                        v-decorator="[
                        'OrganizationUnit',
                        {
                            rules: [
                                { required: true, message: '请输入机构' },
                            ],
                            getValueFromEvent: event => {
                                return this.verify.formalSpace(event)
                            }
                        }
                    ]"
                />
            </a-form-item>
        </a-form>
        <div slot="footer">
            <a-button type="default" @click="cancelApply" :loading="loading">取消</a-button>
            <a-button type="primary" @click="saveApply" :loading="loading">确定</a-button>
        </div>
    </a-modal>
</template>

<script>
    import verify from 'libs/verify'
    import * as pcajs from 'libs/pcaCode'
    export default {
        name: 'createCert',
        data () {
            return {
                form: this.$form.createForm (this),
                verify,
                showAddTable: false,
                loading: false,
                pcaCodeList:[],
                pcaCodeChildrenList:[]
            }
        },
        computed: {
            formStyle () {
                return this.$store.state.common.formStyle
            },
        },
        methods: {
            checkSqFun(data){
                let target = this.pcaCodeList.filter((item)=>item.code === data)
                this.pcaCodeChildrenList = target[0].children
                this.form.setFieldsValue({'LocalityName':this.pcaCodeChildrenList[0].code})
            },
            /**
             * Function    : showModal
             * Description : 打开弹框
             * Input       : none
             * Return      : none
             */
            showModal ( ) {
                this.showAddTable = true
                let _this = this
                setTimeout (function () {
                    _this.$refs[ 'refocus' ].focus ()
                }, 200)
            },
            /**
             * Function    : saveApply
             * Description : 用户信息新增、编辑保存
             * Input       : name
             * Return      : none
             */
            saveApply ( e ) {
                e.preventDefault ()
                this.form.validateFields (( err, value ) => {
                    if (!err) {
                        let _this =this
                        _this.$post('/v1/sm2-csrs',value).then(res => {
                            if (res.code === 0) {
                                _this.$emit('init')
                                _this.$message.success(res.message);
                            } else {
                                _this.$message.error(res.message)
                            }
                        })
                    }
                })
            },

            /**
             * Function    : cancelApply
             * Description : 取消弹出框
             * Input       : name
             * Return      : none
             */
            cancelApply () {
                this.showAddTable = false
                let _this = this
                setTimeout (function () {
                    _this.form.resetFields ()
                    _this.pcaCodeChildrenList = []
                }, 100)
            }
        },

        created () {
            this.pcaCodeList = pcajs.default
        }
    }
</script>
